// components/goodsFilter.js
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    filters: {
      type: Array,
      value: []
    },
    theme: {
      type: String,
      value: 'white'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentFilter: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeFilter(option) {
      const item = option.currentTarget.dataset.item
      let sort = this.data.currentFilter.sort
      if (this.data.currentFilter.name != item.name || item.sort) {
        this.setData({
          currentFilter: {
            name: item.name,
            sort: sort === 'desc' ? 'asc' : 'desc'
          }
        })
        this.triggerEvent('change', item.sort ? `${item.name}_${sort === 'desc' ? 'asc' : 'desc'}` : item.name)
      }
    }
  },
  lifetimes: {
    attached() {
      if (this.data.filters && this.data.filters.length > 0) {
        this.setData({
          currentFilter: this.data.filters[0]
        })
      }
    }
  }
})
